package sg.edu.nus.iss.umpire.service.competition;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import sg.edu.nus.iss.umpire.domain.Umpire;
import sg.edu.nus.iss.umpire.support.HttpMethod;
import sg.edu.nus.iss.umpire.support.Service;
import sg.edu.nus.iss.umpire.support.ServiceConfig;
import sg.edu.nus.iss.umpire.support.ServiceContext;

@ServiceConfig(path = "competitions/new", method = HttpMethod.GET)
public class CreateCompetitionPage implements Service{

    @Override
    public String serve(ServiceContext ctx) throws Exception {
		ctx.getRequest().setAttribute("referee",
				listReferee(ctx.getEntityManager()));
		return "competitions/form";
    }

	private Object listReferee(EntityManager em) {
		String query = "SELECT FROM " + Umpire.class.getName() + " umpire";
		Query q = em.createQuery(query);
		q.setHint("datanucleus.appengine.datastoreReadConsistency", "EVENTUAL");
		List<Umpire> umpires = (List<Umpire>) q.getResultList();
		return umpires;
    }

}
